import { _decorator, Node, Vec3 } from 'cc';
import { WeaponBase } from './WeaponBase';
import { ObjectPool } from '../Core/ObjectPool';
import { Bullet } from './Bullet';

const { ccclass, property } = _decorator;

@ccclass('RangedWeapon')
export class RangedWeapon extends WeaponBase {
    @property
    private bulletSpeed: number = 500;
    
    @property
    private bulletLifetime: number = 2;
    
    @property
    private spread: number = 0;
    
    @property
    private bulletsPerShot: number = 1;
    
    protected attack(): void {
        if (!this._target) return;
        
        // Calculate direction to target
        const direction = new Vec3();
        Vec3.subtract(direction, this._target.worldPosition, this.node.worldPosition);
        direction.normalize();
        
        // Spawn bullet(s)
        for (let i = 0; i < this.bulletsPerShot; i++) {
            this.spawnBullet(direction);
        }
    }
    
    private spawnBullet(direction: Vec3): void {
        // Apply spread if needed
        let bulletDirection = direction.clone();
        if (this.spread > 0 && this.bulletsPerShot > 1) {
            const angle = (Math.random() - 0.5) * this.spread;
            const rad = angle * Math.PI / 180;
            const cosA = Math.cos(rad);
            const sinA = Math.sin(rad);
            
            // Rotate the direction vector
            const x = bulletDirection.x * cosA - bulletDirection.y * sinA;
            const y = bulletDirection.x * sinA + bulletDirection.y * cosA;
            
            bulletDirection.x = x;
            bulletDirection.y = y;
            bulletDirection.normalize();
        }
        
        // Get bullet from object pool
        const bulletNode = ObjectPool.instance.getObject(this.weaponId + "_bullet", this.node.worldPosition);
        
        if (bulletNode) {
            // Set up bullet component
            const bullet = bulletNode.getComponent(Bullet) || bulletNode.addComponent(Bullet);
            
            // Initialize bullet
            bullet.initialize({
                direction: bulletDirection,
                speed: this.bulletSpeed,
                damage: this.damage,
                lifetime: this.bulletLifetime,
                owner: this.node,
                poolKey: this.weaponId + "_bullet"
            });
        }
    }
    
    protected applyUpgradeEffects(effects: string[]): void {
        super.applyUpgradeEffects(effects);
        
        // Apply effects specific to ranged weapons
        if (effects) {
            for (let i = 0; i < effects.length; i++) {
                const effect = effects[i];
                if (effect === 'multishot') {
                    this.bulletsPerShot += 1;
                } else if (effect === 'speed_up') {
                    this.bulletSpeed *= 1.2;
                } else if (effect === 'piercing') {
                    // Add piercing effect - will be handled in Bullet component
                }
            }
        }
    }
} 